package com.school_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school_admin.config.Result;
import com.school_admin.entity.Swiper;
import com.school_admin.service.impl.SwiperServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-04-03
 */
@RestController
@RequestMapping("/swiper")
public class SwiperController {

    private final SwiperServiceImpl swiperService;

    @Autowired
    public SwiperController(SwiperServiceImpl swiperService) {
        this.swiperService = swiperService;
    }

    @GetMapping("/list")
    public Result<?> list(int page, int pageSize) {
        LambdaQueryWrapper<Swiper> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Swiper::getStatus, 0);
        Page<Swiper> page1 = new Page<>(page, pageSize);
        IPage<Swiper> list = swiperService.page(page1, queryWrapper);
        List<Swiper> records = list.getRecords();
        int total = (int) list.getTotal();
        Map map = new HashMap<>();
        map.put("list", records);
        map.put("total", total);
        return Result.success(map);
    }

    @PostMapping("/create")
    public Result<?> add(Swiper swiper) {
        boolean is = swiperService.save(swiper);
        if (is) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    @PutMapping("/updateStatus/{id}/{status}")
    public Result<?> updateStatus(@PathVariable("id") Integer id,
                                  @PathVariable("status") Integer status) {
        Swiper swiper = new Swiper();
        swiper.setId(id);
        swiper.setStatus(status);
        boolean is = swiperService.updateById(swiper);
        if (is) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    // 删除
    @DeleteMapping("delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        boolean d = swiperService.removeById(id);
        if (d) {
            return Result.success();
        }
        return Result.fail();
    }

    @GetMapping("/web/list")
    public Result<List<Swiper>> webList() {
        LambdaQueryWrapper<Swiper> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Swiper::getStatus, 0);
        List<Swiper> list = swiperService.list(wrapper);
        return Result.success(list);
    }
}
